/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterScreen.java
 *
 * Created on Mar 30, 2011, 4:31:26 PM
 */

package forumGUI;

import client.ForumClientListener;
import client.TcpClient;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import messaage.*;

/**
 * 
 * @author Olga
 */
public class RegisterScreen extends javax.swing.JFrame implements
		client.MessagesListener {

	private TcpClient client;
	JFrame parentWindow;

	/** Creates new form RegisterScreen */
	public RegisterScreen(TcpClient _client, JFrame _parentWindow) {
		this.client = _client;
		this.parentWindow = _parentWindow;
		initComponents();
                 this.setBackground();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RegisterLabel = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        genderLabel = new javax.swing.JLabel();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        firstNameTextField = new javax.swing.JTextField();
        lastNameTextField = new javax.swing.JTextField();
        emailTextField = new javax.swing.JTextField();
        genderComboBox = new javax.swing.JComboBox();
        usernameTextField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        RegisterButton = new javax.swing.JButton();
        back = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                windowClosing(evt);
            }
        });

        RegisterLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 48));
        RegisterLabel.setText("Register");

        firstNameLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        firstNameLabel.setText("First name:");

        lastNameLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        lastNameLabel.setText("Last name:");

        emailLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        emailLabel.setText("e-mail:");

        genderLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        genderLabel.setText("gender:");

        usernameLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        usernameLabel.setText("username:");

        passwordLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        passwordLabel.setText("password:");

        genderComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "male", "female" }));

        RegisterButton.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        RegisterButton.setText("Register!");
        RegisterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegisterButtonActionPerformed(evt);
            }
        });

        back.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        back.setText("back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lastNameLabel)
                                    .addComponent(firstNameLabel))
                                .addGap(64, 64, 64))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(genderLabel)
                                    .addComponent(emailLabel)
                                    .addComponent(usernameLabel)
                                    .addComponent(passwordLabel))
                                .addGap(68, 68, 68)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(genderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(emailTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(lastNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(firstNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(passwordField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(RegisterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(back, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(RegisterLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(187, 187, 187))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(RegisterLabel)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(firstNameLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lastNameLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(emailLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(genderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(genderLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(usernameLabel))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(passwordLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(186, 186, 186)
                        .addComponent(RegisterButton)
                        .addGap(49, 49, 49)
                        .addComponent(back)))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
          this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
          DisconnectMsg msg = new DisconnectMsg(null);  //TODO change the username to send
          this.client.sendMessage(msg);
        }//GEN-LAST:event_formWindowClosed

	private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_RegisterButtonActionPerformed
		String username = this.usernameTextField.getText();
		String password = this.passwordField.getText();
		String fName = this.firstNameTextField.getText();
		String lName = this.lastNameTextField.getText();
		String mail = this.emailTextField.getText();
		String gender = (String) this.genderComboBox.getSelectedItem();
		;
		char gen = 'm'; // initial value
		if (gender.equals("male")) {
			gen = 'm';
		} else if (gender.equals("female")) {
			gen = 'f';
		} else {
			JOptionPane.showMessageDialog(this, "Please select a gender.");
		}

		if (!username.equals("") && !password.equals("") && !fName.equals("")
				&& !lName.equals("") && !mail.equals("")) {
			RegisterMsg msg = new RegisterMsg(fName, lName, mail, gen,
					username, password);
			this.client.sendMessage(msg);
		} else
			JOptionPane.showMessageDialog(this,
					"Missing information. Please fill the fields again.");

	}// GEN-LAST:event_RegisterButtonActionPerformed

	private void backActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_backActionPerformed
		this.setVisible(false);
		this.parentWindow.setVisible(true);
	}// GEN-LAST:event_backActionPerformed
        
      public void setBackground()
    {
        Container con =this.getContentPane();
        JPanel panelBgImg;
        con.setLayout(null);
        final String photoPath = "src/images/screen.jpg";
        ImageIcon imh = new ImageIcon(photoPath);
//        setSize(imh.getIconWidth(), imh.getIconHeight());

        panelBgImg = new JPanel()
        {
            public void paintComponent(Graphics g)
            {
                Image img = new ImageIcon(photoPath).getImage();

                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            }
        };

        con.add(panelBgImg);
        panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

    }
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				// new RegisterScreen().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton RegisterButton;
    private javax.swing.JLabel RegisterLabel;
    private javax.swing.JButton back;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JComboBox genderComboBox;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JTextField lastNameTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables

	@Override
	public void registered(RegisteredMsg msg) {
		String usrename = this.usernameTextField.getText();
        boolean isModerator = false;
		forumsScreen fs = new forumsScreen(this.client, usrename, isModerator);
		ForumClientListener tempListener = (ForumClientListener) this.client
				.getListener();
		tempListener.getConnector().addListener(fs);
		String body = "Registered successfully";
		JOptionPane.showMessageDialog(this, body);
		
		this.setVisible(false);
		this.parentWindow.setVisible(true);
	}

	@Override
	public void loggedIn(LoggedInMsg msg) {

	}

	@Override
	public void errorMessage(ErrorMsg msg) {
		String body = msg.getBody();
		JOptionPane.showMessageDialog(this, body);
	}

	@Override
	public void connected(ConnectedMsg msg) {

	}

	@Override
	public void disconnected(DisconnectedMsg msg) {
                this.dispose();
	}

	@Override
	public void loggedOff(LoggedOffMsg msg) {

	}

	@Override
	public void gotListOfForums(GotListOfForumsMsg msg) {

	}

	@Override
	public void gotTopicsOfForum(GotTopicsOfForumMsg msg) {

	}

	@Override
	public void gotThread(GotThreadMsg msg) {

	}

	@Override
	public void addedPost(AddedPostMsg msg) {
	}

	@Override
	public void createdThread(CreatedThreadMsg msg) {

	}

	@Override
	public void addedForum(AddedForumMsg msg) {

	}

	@Override
	public void removedForum(RemovedForumMsg msg) {

	}

	@Override
	public void editedPost(EditedPostMsg msg) {

	}

	@Override
	public void removedUser(RemovedUserMsg msg) {

	}

	@Override
	public void removedThread(RemovedThreadMsg msg) {

	}

	@Override
	public void removedPost(RemovedPostMsg msg) {

	}

	@Override
	public void addedFriend(AddedFriendMsg msg) {

	}

	@Override
	public void removedFriend(RemovedFriendMsg msg) {

	}

	@Override
	public void gotFriendsList(GotFriendsListMsg msg) {
	}

}
